//
//  AppDelegate.m
//  xcode717SandBox
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //    SandBox 沙盒
    
    //    Documents(文件文档，用户数据存储)
    //    library （资源，一般用来存放，程序员要存储的数据）
    //    ↓
    //    caches（缓存文件）
    //     preferences (用户信息，用户设置。NSUserDefaults)
    //    tmp (临时目录，下载的临时文件一般放这里)
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    //    2.获取沙盒路径
    //    快捷获取对应目录的c函数
    //    根目录 家目录
    NSLog(@"%@", NSHomeDirectory());
    //    临时目录 tmp
    NSLog(@"%@", NSTemporaryDirectory());
    //    NSSearchPathDirectory
    //    参数1：搜索那个文件夹路径
    //    参数2：在用户作用域下搜索
    //    参数3：yes或no 分别代表绝对的路径（基本都用绝对，）no代表相对路径
    NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //     NSLog(@"%@",[pathArr lastObject]);
    
    
    
    //    nsbudle .app文件包
    //    NSLog(@"%@",[NSBundle mainBundle]);
    
    //    简单的文件读写 input output
    NSString *helloString = @"Hello,I/O File";
    //    NSString *writePath = [[pathArr firstObject] stringByAppendingString:@"hello.txt"];
    NSError *error = nil;//指向指针的指针
    NSString *writePath = [[pathArr firstObject] stringByAppendingPathComponent:@"hello.txt"];
    BOOL isSuccess = [helloString writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
//        NSLog(@"存储失败");
    } else
    {
//        NSLog(@"恭喜存储成功");
    }
    
    //    读取路径对应的文字
    NSError *readError = nil;
    NSString *readString = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
    //    NSLog(@"%@",readString);
    
    //   数组读写，将数组写入本地文件
    NSArray *array = @[@"挖与到",@"挖到发个",@"如图到",];
    NSArray *array2 = @[@"挖都是到",@"挖到好几个",@"挖到",];
    NSArray *array3 = @[@"挖到二位",@"挖根号到",@"挖你们到",];
    
    NSString *arrayPath = [[pathArr firstObject] stringByAppendingPathComponent:@"name.plist"];
    BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES];
    if (isArrayWriteSuccess) {
        //         NSLog(@"写入成功");
    } else {
        //         NSLog(@"写入失败");
    }
    
    //    将数组读入
    NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath];
//    NSLog(@"%@",nameArray);
    
    
//   将字典写入本地文件
    NSDictionary *dic = @{@"科比":@"name",@"37":@"age"};
    NSString *dicpath = [[pathArr firstObject] stringByAppendingPathComponent:@"dic.plist"];
    BOOL isDicWriteSuccess = [dic writeToFile:dicpath atomically:YES];
    if (isDicWriteSuccess) {
//        NSLog(@"写入成/功");
    } else {
//        NSLog(@"写入失败");
    }
//    将字典读入
    NSDictionary *dicContent = [NSDictionary dictionaryWithContentsOfFile:dicpath];
//    NSLog(@"%@",dicContent);


//    将data类型写入本地
    
    UIImage *image = [UIImage imageNamed:@"0"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0);
    UIImagePNGRepresentation(image);
//    NSData *data = [UIImagePNGRepresentation(image)];
//    NSLog(@"%@",imageData);
    NSString *dataPath = [[pathArr firstObject] stringByAppendingPathComponent:@"imageData"];
    BOOL isDataWriteSuccess = [imageData writeToFile:dataPath atomically:YES];
    if (isDataWriteSuccess) {
//         NSLog(@"写入成功");
    } else {
//         NSLog(@"写入失败");
    }
//  nsdata 类型读入
UIImage *fileImage = [UIImage imageWithData:imageData];
NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath];
    
//    2复杂对象文件读写，自定义类型
//  归档反归档，序列化反序列化
    
//    存储，将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"多情剑客无情剑";
//    book.bookType = @"武侠小说";
    book.bookPrice = @"87";
//    book.bookAuthor = @"古龙";
//    book.bookAddress = @"台湾出版社";
    
    NSString *bookPath = [[pathArr firstObject] stringByAppendingPathComponent:@"book.plist"];
    BOOL isBookWriteSuccess = [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBookWriteSuccess) {
        NSLog(@"写入成功");
    } else {
         NSLog(@"失败");
    }
    
    
//    反归档,取出本地存储的对象（反归档走的是初始化方法）
  Book *guBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
     NSLog(@"%@",guBook.bookName);
    
//    如果对象想要实现归档和反归档
//    对象对应的类需要签订《NSCoding》
//    实现协议方法
//    initwithacoder 反归档用的
//    encodewithcoder 归档用的
//    归档时使用NSKeyedArchiver
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
